package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.service.IEvaluationDetailsService;
import com.cqut.model.EvaluationDetails;
import com.cqut.dao.EvaluationDetailsMapper;
import com.cqut.dto.LimitShowDTO;
import com.cqut.util.SqlUtil;

@Service
public class EvaluationDetailsService implements IEvaluationDetailsService {
    @Resource(name="evaluationDetailsMapper")
    private EvaluationDetailsMapper evaluationDetailsMapper;

    @Override
    public boolean save(EvaluationDetails evaluationDetails) {
        return evaluationDetailsMapper.insert(evaluationDetails) > 0;
    }

    @Override
    public boolean update(EvaluationDetails evaluationDetails) {
        return evaluationDetailsMapper.updateByPrimaryKeySelective(evaluationDetails) >= 0;
    }

    @Override
    public boolean delete(String evaluationId) {
        return evaluationDetailsMapper.deleteByPrimaryKey(evaluationId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return evaluationDetailsMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public EvaluationDetails findModel(String targetId) {
        return evaluationDetailsMapper.selectByPrimaryKey(targetId);
    }

    @Override
    public EvaluationDetails findModel(String targetId, String[] parameters) {
        return evaluationDetailsMapper.findModel(targetId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String targetId) {
        String[][] parameters = {{"target_id", "targetId"}, {"target_name", "targetName"}, {"parent_id", "parentId"}, {"hassubordinate", "hassubordinate"}, {"target_score", "targetScore"}, {"target_remark", "targetRemark"}, {"creater_id", "createrId"}, {"creater_name", "createrName"}, {"creater_time", "createrTime"}, {"evaluation_id", "evaluationId"}};
        return this.findMap(targetId, parameters);
    }

    public Map<String, Object> findMap(String targetId, String[][] parameters) {
        return evaluationDetailsMapper.findMap(targetId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<EvaluationDetails> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<EvaluationDetails> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<EvaluationDetails> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return evaluationDetailsMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"target_id", "targetId"}, {"target_name", "targetName"}, {"parent_id", "parentId"}, {"hassubordinate", "hassubordinate"}, {"target_score", "targetScore"}, {"target_remark", "targetRemark"}, {"creater_id", "createrId"}, {"creater_name", "createrName"}, {"creater_time", "createrTime"}, {"evaluation_id", "evaluationId"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"target_id", "targetId"}, {"target_name", "targetName"}, {"parent_id", "parentId"}, {"hassubordinate", "hassubordinate"}, {"target_score", "targetScore"}, {"target_remark", "targetRemark"}, {"creater_id", "createrId"}, {"creater_name", "createrName"}, {"creater_time", "createrTime"}, {"evaluation_id", "evaluationId"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return evaluationDetailsMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return evaluationDetailsMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }
    
    /**
     * 获得评估指标evaluationList
     * **/
    public List<Map<String, Object>> getEvaluationList(String parentId,String evaluationId,String evaluationName,LimitShowDTO limitShowDTO){
    	return evaluationDetailsMapper.getEvaluationListByEvaluationName(parentId,evaluationId,evaluationName,limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
    }
    
    /**
     * 获得评估指标evaluationCount
     * **/
    public long getEvaluationListCount(String parentId,String evaluationId,String editionName){
    	return evaluationDetailsMapper.getCountByEvaluationName(parentId,evaluationId,editionName);
    }
    
    /**
     * 新增指标
     * **/
    public boolean addEvaluationDeatils(EvaluationDetails evaluationDetails){
    	evaluationDetails.setTargetId(getNewTargetId());
    	
    	String userName = evaluationDetailsMapper.getNameByUserId(evaluationDetails.getCreaterId());
    	evaluationDetails.setCreaterName(userName);
    	evaluationDetails.setHassubordinate(0);
    	
    	return evaluationDetailsMapper.insert(evaluationDetails)>0;
    }
    
    /**
     * 获得新id
     * **/
    public String getNewTargetId(){
    	Map<String, Object> maxId = evaluationDetailsMapper.getMaxTargetId();
    	
		String temp = maxId.values().toString();
		
		int max = Integer.parseInt(temp.substring(1,temp.length()-1));
		max++;
		String maxNum = Integer.toString(max);
		switch(maxNum.length()){
		case 1: return "000"+max;
		case 2: return "00"+max;	
		case 3: return "0"+max;
		}
		return "";
    }
    
    /**
     * 根据评审id获得详情
     * **/
    public Map<String, Object> getEvaluationDeatilsById(String targetId){
    	return evaluationDetailsMapper.findDetailById(targetId);
    }

    /**
     * 修改评审指标
     * **/
    public boolean updateEvaluation(EvaluationDetails evaluationDetails){
    	return evaluationDetailsMapper.updateByPrimaryKeySelective(evaluationDetails)>0;
    }
    
    /**
     * 获得版本使用情况
     * **/
    public List<Map<String, Object>> getUsageList(String reviewVersionId,String competitionName,LimitShowDTO limitShowDTO){
    	return evaluationDetailsMapper.findUsageById(reviewVersionId,competitionName,limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
    }
    
    public long getUsageListCount(String reviewVersionId, String competitionName){
    	return evaluationDetailsMapper.findUsageCount(reviewVersionId,competitionName);
    }

	@Override
	public boolean deleteWholeEvaluation(String evaluationId) {
		// TODO Auto-generated method stub
		boolean delete_1 = evaluationDetailsMapper.deleteWholeEvaluation_1(evaluationId)>0;
		boolean delete_2 = evaluationDetailsMapper.deleteWholeEvaluation_2(evaluationId)>0;
		return (delete_1 && delete_2);
	}

	@Override
	public List<Map<String, Object>> getVersionList(String evaluationId) {
		// TODO Auto-generated method stub
		
		return evaluationDetailsMapper.getVersionListById(evaluationId);
	}

	@Override
	public boolean deleteOneEvaluation(String evaluationId) {
		// TODO Auto-generated method stub
		return evaluationDetailsMapper.deleteOneEvaluation(evaluationId)>0;
	}
}
